package com.geeklan.smart.data.repository.user;

import androidx.annotation.NonNull;

import com.geeklan.smart.data.auth.AuthManager;
import com.geeklan.smart.data.model.ApiResponse;
import com.geeklan.smart.data.model.LoginResponse;
import com.geeklan.smart.data.model.User;
import com.geeklan.smart.data.remote.RetrofitClient;
import com.geeklan.smart.data.remote.user.UserApiService;
import com.geeklan.smart.data.repository.RepositoryCallback;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AccountRepository {
    private final UserApiService apiService;

    public AccountRepository() {
        this.apiService = RetrofitClient.getInstance().getUserApiService();
    }

    public void login(User user, RepositoryCallback<ApiResponse<LoginResponse>> callback) {
        Call<ApiResponse<LoginResponse>> call = apiService.login(user);
        call.enqueue(new Callback<ApiResponse<LoginResponse>>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse<LoginResponse>> call, @NonNull Response<ApiResponse<LoginResponse>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse<LoginResponse>> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void register(User user, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.register(user);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful()) {
                    callback.onSuccess(response.body());
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void updatePassword(User user, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.updatePassword(user);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void removeUser(RepositoryCallback<ApiResponse> callback) {
        Map<String, Object> requestBody = new HashMap<>();
        Call<ApiResponse> call = apiService.removeUser(requestBody);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void tokenLogin(User user, RepositoryCallback<User> callback) {
        Call<ApiResponse<User>> call = apiService.tokenLogin(user);
        call.enqueue(new Callback<ApiResponse<User>>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse<User>> call, @NonNull Response<ApiResponse<User>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body().getData());
                    }else {
                        AuthManager.getInstance().logoutAndRedirectToLogin();
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse<User>> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void updateUserInfo(User user, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.updateUserInfo(user);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        AuthManager.getInstance().logoutAndRedirectToLogin();
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void getUserInfoByToken(RepositoryCallback<ApiResponse<User>> callback) {
        Call<ApiResponse<User>> call = apiService.getUserInfoByToken();
        call.enqueue(new Callback<ApiResponse<User>>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse<User>> call, @NonNull Response<ApiResponse<User>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        AuthManager.getInstance().logoutAndRedirectToLogin();
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse<User>> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }
}
